from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

# 导入又拍云
import upyun

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F 

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

from myapp.models import *

# 导入redis库
import redis

#定义地址和端口
host = '127.0.0.1'
port = 6379

#建立redis连接
r = redis.Redis(host=host,port=port)


# 导入序列化类
from myapp.myser import *








#格式化结果集
def dictfetch(cursor):

	#声明描述符
	desc = cursor.description

	return [ dict( zip([col[0] for col in desc ],row) ) 

			for row in cursor.fetchall()
	]




# 搜索接口
class Search(APIView):
    def get(self,request):
         # 检索字段
        text = request.GET.get('text',None)

        # 转换数据类型
        text = json.loads(text)

        sql = ""
        # 动态拼接
        for val in text:

            sql += "or name like '%%%s%%' " % val


        sql = sql.lstrip("or")

        sql_cursor = " select name,id,img,price from goods where id != 0 and ( " + sql + ")" 

        print(sql)

        # 建立游标对象
        cursor = connection.cursor()

        # 执行sql语句
        cursor.execute(sql_cursor)

        #查询
        # result = cursor.fetchall()

        result = dictfetch(cursor)

        print(result)

        return Response({
            'data':result
        })




# # 获取用户信息
class UserList(APIView):
    def get(self,requests):
        
        # 查询
        user = User.objects.filter()
        # print(user)

        # 序列化
        user_ser = UserSer(user,many=True)
        # print(user_ser)

        return Response({
            'data': user_ser.data
        })




#商品评论获取
class COmmentList(APIView):
    def get(self,request):

        gid = request.GET.get("gid",None)



        # 查询数据
        comments = Comment.objects.filter(gid=gid).order_by("-id")

    

        # 序列化
        comments_ser = CommentSer(comments,many=True)

        return Response({
            'data':comments_ser.data
        })




# 商品评论
class CommentInsert(APIView):
    def post(self,request):

        # 获取客户端IP
        if 'HTTP_X_FORWARDED_FOR' in request.META:

            ip = request.META.get('HTTP_X_FORWARDED_FOR')
        else:
            ip = request.META.get('REMOTE_ADDR')

        uid = request.POST.get('uid',None)

        if r.get(ip):

        # if r.llen(ip) > 3:

            return Response({
            'code':400,
            'message':'请稍候评论'
            })
            

        # 初始化参数
        comment = CommentSer(data=request.data)

        # 验证字段
        if comment.is_valid():

            # 进行入库
            comment.save()

            # 设置评论间隔时间
            r.set(ip,"123")
            r.expire(ip,30)

        return Response({
            'code':200,
            'message':'评论成功'
        })




# 商品详情页
class GoodInfo(APIView):
    def get(self,request):

        id = request.GET.get('id')

        # 查询
        good= Goods.objects.get(id=id)

        # 序列化
        good_ser = GoodsSer(good)
        
        # redis 操作存入图片
        # r.lpush(good.name,'21.jpg')
        # r.lpush(good.name,'14.jpg')
        # r.lpush(good.name,'12.jpg')
        # r.lpush(good.name,'12.jpg')

  
        # 建立一个空列表  用以接收数据
        re = []
        # 循环 数据   
        # 用id会有问题   部分参数无法接收  再次修改为name
        # for i in r.lrange(good.name,0,5):
        for i in r.lrange(good.name,0,4):
            # 以utf-8格式依此添加列表
            print(i)
            re.append(i.decode('utf-8'))

        return Response({
            'data':good_ser.data,
            're':re
        })



# 商品列表页
class GoodsList(APIView):
    def get(self,request):

        # 检索字段
        text = request.GET.get('text',None)

        print(text)
        #排序字段
        coloum = request.GET.get('coloum',None)
        #排序方法
        sort_order = request.GET.get('order','')
        # 当前页
        page = request.GET.get('page',1)
        # 一页显示个数
        size = request.GET.get('size',3)

        # 计算从哪开始切
        data_start = (int(page)-1) * int(size)

        # 计算切到哪
        data_end = int(page) * int(size)

        # 查询  切片操作
        if coloum:

            goods = Goods.objects.all().order_by(sort_order+coloum)[data_start:data_end]
        else:
            goods = Goods.objects.all()[data_start:data_end]

       	#判断是否进行模糊查询
        if text:
            goods = Goods.objects.filter(Q(name__contains=text) | Q(desc__contains=text) )[data_start:data_end]

        

            count = Goods.objects.filter(Q(name__contains=text) | Q(desc__contains=text)).count()

            

        else:

            #查询所有商品个数
            count = Goods.objects.count()


        # 序列化
        goods_ser = GoodsSer(goods,many=True)

        return Response({

            'data':goods_ser.data,
            'total':count

        })



# 商品分类列表
class CategoryList(APIView):
    def get(self,request):

        category = Category.objects.all()

         # 序列化操作
        category_ser =  CategorySer(category,many=True)

        return Response(category_ser.data)



# 商品入库接口
class InsertGoods(APIView):
    def get(self,request):

        # 接参
        name = request.GET.get('name',None)

        price = request.GET.get('price',None)

        params = request.GET.get('params',None)

        cid = request.GET.get('cid',None)

        # 排重操作
        goods = Goods.objects.filter(name=name).first()

        if goods:
            return Response({
                'code': 401,
                'message': '已经添加过此商品'
            })

        # 入库

        goods = Goods(name=name,price=price,params=params,cid=int(cid))
        goods.save()

        # 获取已存入的商品信息
        goods = Goods.objects.filter(name=name).first()
        # 存入redis中
        r.set(goods.id,params)

        return Response({
            'code': 200,
            'message': '添加商品成功'
        })

























